//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace LitwareHR.Portfolio.DataModel.Sql
{
    public class ProvisioningDataModel
    {      
        public void PerformModulesProvisioning(Guid tenantId, string tenantName, string tenantAlias)
        {
            ProvisionModule(tenantId, tenantName, tenantAlias, "Runtime", Constants.DB.TenantMetadataStore);
            ProvisionModule(tenantId, tenantName, tenantAlias, "Recruiting", Constants.DB.TenantMetadataStore);
            ProvisionModule(tenantId, tenantName, tenantAlias, "Recruiting", Constants.DB.TenantDataStore);
        }

        public void ProvisionSilverlightStreaming(Guid tenantId, string silverlightAccountId, string silverlightAccountKey)
        {
            Database db = DatabaseFactory.CreateDatabase(Constants.DB.TenantMetadataStore);
            using (DbCommand command = db.GetStoredProcCommand("dbo.ProvisionSilverlightStreaming"))
            {
                db.AddInParameter(command, "tenantId", DbType.Guid, tenantId);
                db.AddInParameter(command, "accountId", DbType.String, silverlightAccountId.Trim());
                db.AddInParameter(command, "accountKey", DbType.String, silverlightAccountKey.Trim());
                db.ExecuteNonQuery(command);
            }
        }

        private void ProvisionModule(Guid tenantId, string tenantName, string tenantAlias, string module, string databaseString)
        {
            Database db = DatabaseFactory.CreateDatabase(databaseString);
            using (DbCommand command = db.GetStoredProcCommand("dbo.Provision" + module))
            {
                db.AddInParameter(command, "tenantId", DbType.Guid, tenantId);
                db.AddInParameter(command, "tenantName", DbType.String, tenantName);
                db.AddInParameter(command, "tenantAlias", DbType.String, tenantAlias);
                db.ExecuteNonQuery(command);
            }
        }
    }
}
